﻿using ConfExpert.Forms.Base;

using DevExpress.XtraEditors.Repository;
using DevTeam;


using System;
using System.Data;
using System.Drawing;

namespace ConfExpert.Forms.Depo
{
    public partial class MKControl : XForm
    {
        public MKControl()
        {
            InitializeComponent();
        }

        private void MKControl_Load(object sender, EventArgs e)
        {
            
            DataTable dtCust = Session.DataManager.ExecuteDataTable("GetMyCustomers2", WhoAmI);
            RepositoryItemGridLookUpEdit riCust = GetCustomGLE(dtCust, "Name", "CustomerId");

            gleMusteri.Edit.Assign(riCust);
            gleMusteri.EditValueChanged += gleMusteri_EditValueChanged;
            ((RepositoryItemGridLookUpEdit)gleMusteri.Edit).TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor;

            gv.RowStyle += gridView1_RowStyle;

            GridManager gm = GridManager.GetManager(gv);
            gm.GridMenu.AddMenu("Kaydet", Kaydet);

            dt = new DataTable("MK");
            dt.Columns.Add("MusteriKodlariId", typeof(int));
            dt.Columns.Add("ProductId", typeof(int));
            dt.Columns.Add("MusteriKodu", typeof(string));
        }

        void gleMusteri_EditValueChanged(object sender, EventArgs e)
        {

            gv.Init("GetMKControl", intParse(gleMusteri.EditValue));

            gv.OptionsView.EnableAppearanceOddRow = gv.OptionsView.EnableAppearanceEvenRow = false;
            gv.MakeReadOnly("MusteriKodu");
            gv.AddSummary();
            gv.BestFitColumns();
        }

        DataTable dt;
        void Kaydet(object sender, EventArgs e)
        {
            gv.CloseEditor();
            gv.UpdateCurrentRow();

            dt.Clear();

            foreach (int rowHandle in gv.GetSelectedRows())
            {
                DataRow dr = gv.GetDataRow(rowHandle);

                dt.Rows.Add(intParse(dr["MusteriKodlariId"]), intParse(dr["ProductId"]), strParse(dr["MusteriKodu"]));
            }

            gv.RefreshDataSource(true, "UpdMKControl", intParse(gleMusteri.EditValue), WhoAmI, dt.SerializeChanges());
            gv.BestFitColumns();

            //if (rowHandle > -1)
            //    dosyalar += gv.GetRowCellValue(rowHandle, "MusteriKodlariId").ToString() + ",";

            //if (dosyalar.Substring(dosyalar.Length - 1, 1) == ",")
            //    dosyalar = dosyalar.Substring(0, dosyalar.Length - 1);

            //gv.Update("");
        }

        void gridView1_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
        {
            if (e.RowHandle > -1)
            {
                if (gv.GetRowCellValue(e.RowHandle, "KontrolTarihi") != DBNull.Value)
                    e.Appearance.BackColor = e.Appearance.BackColor2 = Color.SkyBlue;
                else
                    e.Appearance.Combine((e.RowHandle & 1) != 0 ? gv.Appearance.EvenRow : gv.Appearance.OddRow);
            }
        }

    }
}
